package program;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class MainJFrame extends JFrame {
	private static final long serialVersionUID = 1393296138352062699L;
	
	private Properties frameProperties = new Properties();

	
	/**
	 * Create the main window and load old settings
	 */
	public MainJFrame() {
		setTitle("Kampo");
		
		//Load old settings or use default properties
		if(!readLastProperties()) {
			this.setSize(500, 500);
			this.setLocation(0, 0);
		}
		
		//Add closing operation that saves properties
		addClosingOperation();
		
		//Create the menu
		createMenu();
		
		//Show window
		setVisible(true);
	}
	
	/**
	 * Closing operation that saves properties to file:
	 * - width, height of window
	 * - position of window
	 */
	private void addClosingOperation() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				//Save JFrame properties
				frameProperties.put("width", ""+getSize().width);
				frameProperties.put("height", ""+getSize().height);
				frameProperties.put("posX", ""+getLocation().x);
				frameProperties.put("posY", ""+getLocation().y);
				
				try {
					FileWriter writer = new FileWriter( "config.txt" ); 
					frameProperties.store(writer, null);
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				//Finally exit
				System.exit(0);
			}
		});
	}
	
	/**
	 * Load the last properties from file
	 * @return true when successful loaded last properties
	 */
	private boolean readLastProperties() {
		Reader reader;
		try {
			reader = new FileReader( "config.txt" );
			frameProperties.load( reader );
			
			//Restore old size and location
			this.setSize(Integer.parseInt(frameProperties.get("width").toString()), 
						 Integer.parseInt(frameProperties.get("height").toString()));
			this.setLocation(Integer.parseInt(frameProperties.get("posX").toString()), 
							 Integer.parseInt(frameProperties.get("posY").toString()));
		} catch (FileNotFoundException e) {
			//File not found...
			return false;
		} catch (IOException e) {
			//...or not readable
			return false;
		} 
		
		return true;
	}
	
	/**
	 * Create the menu
	 */
	private void createMenu() {
		MenuListener menuLis = new MenuListener();
		
		//File menu
		JMenu mFile = new JMenu("File");
	    JMenuItem mNew = new JMenuItem("New"),
	    		  mOpen = new JMenuItem("Open..."),
	              mSave = new JMenuItem("Save..."),
	              mExit = new JMenuItem("Exit");
	    
	    //Help menu
	    JMenu mHelp = new JMenu("Help");
    	JMenuItem mSearch = new JMenuItem("Search..."),
    			  mAbout = new JMenuItem("About");

    	//Add ActionCommand and listener
	    mNew.setActionCommand("New");            mNew.addActionListener(menuLis);
	    mOpen.setActionCommand("open");          mOpen.addActionListener(menuLis);
	    mSave.setActionCommand("save");          mSave.addActionListener(menuLis);
	    mExit.setActionCommand("exit");          mExit.addActionListener(menuLis);
	    
	    mSearch.setActionCommand("search");      mSearch.addActionListener(menuLis);         
	    mAbout.setActionCommand("about");        mAbout.addActionListener(menuLis);

	    //Put everything together
	    mFile.add(mNew);
	    mFile.add(mOpen);
	    mFile.add(mSave);
	    mFile.add(mExit);
	    
	    mHelp.add(mSearch);
	    mHelp.add(mAbout);
	    
	    JMenuBar mBar = new JMenuBar();
	    mBar.add(mFile);
	    mBar.add(mHelp);
	    
	    //Add menu to window
	    this.setJMenuBar(mBar);
	}
}
